package silhouette

import com.mohiva.play.silhouette.api.actions.SecuredErrorHandler
import play.api.mvc.{ PlayBodyParsers, RequestHeader, Result }
import cats.implicits._
import models.TableData
import play.api.http.Status
import utils.CusResult

import scala.concurrent.{ ExecutionContext, Future }

/**
 * ajax 请求权限错误重定向
 */
class JsonSecuredErrorHandler(parse1: PlayBodyParsers)(
  implicit
  ec: ExecutionContext)
  extends SecuredErrorHandler
  with CusResult {

  override val parse: PlayBodyParsers = parse1

  override def onNotAuthenticated(
    implicit
    request: RequestHeader): Future[Result] = {
    /*futureLess.lift {
      val content = futureLess.unlift(R.toJson(V.leftT[String](ErrorResult("用户未登录"))))
      Results.Unauthorized(content)
    }*/
    R.toJsonResult(
      V.leftT[TableData[Option[String]]](
        ErrorResult("用户未登录", code = Status.UNAUTHORIZED)))
  }
  override def onNotAuthorized(
    implicit
    request: RequestHeader): Future[Result] = {
    /*futureLess.lift {
      val content = futureLess.unlift(R.toJson(V.leftT[String](ErrorResult("你没有足够的权限进行此操作"))))
      Results.Forbidden(content)
    }*/
    R.toJsonResult(
      V.leftT[TableData[Option[String]]](
        ErrorResult("你没有足够的权限进行此操作", code = Status.FORBIDDEN)))
  }

}
